
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Photo {

    private File photoFile;
    private String photoName;
    private int width;
    private int height;
    private Image imgI;
    private int index;
    private int position_x;

    public Photo(File photoFile,
            Image imgIcon,
            String photoName,
            int width, int height) {

        this.photoFile = photoFile;
        this.imgI = imgIcon;
        this.photoName = photoName;
        this.width = width;
        this.height = height;
    }

    public Photo(String url) throws IOException {

        this.photoFile = new File(url);

        this.imgI = ImageIO.read(this.photoFile);
        this.photoName = url;
        this.width = imgI.getWidth(null);
        this.height = imgI.getHeight(null);
    }

    public File getOriginalPhotoURL() {
        return photoFile;
    }

    public void setOriginalPhotoURL(File photoFile) {
        this.photoFile = photoFile;
    }

    public String getPhotoName() {
        return photoName;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String toString() {
        return photoFile + "\t" + imgI + "\t" + photoName + "\t" + width + "\t" + height;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getPosition_x() {
        return position_x;
    }

    public void setPosition_x(int position_x) {
        this.position_x = position_x;
    }

    public Image getImgIcon() {
        return imgI;
    }
 public ImageIcon getIcon() {
        ImageIcon icon = new ImageIcon(imgI);
        return icon;
    }
    public void setImgIcon(Image imgIcon) {
        this.imgI = imgIcon;
    }

    public void createThumbnail() {
        try {
            // BufferedImage is the best (Toolkit images are less flexible)
           // BufferedImage img = ImageIO.read(file);
            BufferedImage thumb = createEmptyThumbnail();

            // BufferedImage has a Graphics2D
            Graphics2D g2d = (Graphics2D) thumb.getGraphics();
            g2d.drawImage(imgI, 0, 0,
                    thumb.getWidth() - 1,
                    thumb.getHeight() - 1,
                    0, 0,
                    imgI.getWidth(null) - 1,
                    imgI.getHeight(null) - 1,
                    null);
            g2d.dispose();
            ImageIO.write(thumb, "PNG", createOutputFile(photoFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public ImageIcon getThumbnail() {
        try {
            // BufferedImage is the best (Toolkit images are less flexible)
           // BufferedImage img = ImageIO.read(file);
            BufferedImage thumb = createEmptyThumbnail();
            
            // BufferedImage has a Graphics2D
            Graphics2D g2d = (Graphics2D) thumb.getGraphics();
            g2d.drawImage(imgI, 0, 0,
                    thumb.getWidth() - 1,
                    thumb.getHeight() - 1,
                    0, 0,
                    imgI.getWidth(null) - 1,
                    imgI.getHeight(null) - 1,
                    null);
            g2d.dispose();
            ImageIO.write(thumb, "PNG", createOutputFile(photoFile));
            
            ImageIcon icon = new ImageIcon(thumb);
            return icon;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private File createOutputFile(File inputFile) {
        // You'll want something better than this...
        return new File(inputFile.getAbsolutePath()
                + ".thumb.png");
    }

    private BufferedImage createEmptyThumbnail() {
        return new BufferedImage(90, 90,
                BufferedImage.TYPE_INT_RGB);
    }

  
}
